package com.eastshop.order.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.eastshop.order.vo.Order;
import com.eastshop.order.vo.Orderitem;
import com.eastshop.utils.PageHibernateCallback;
/**
 * 订单管理的Dao层代码
 * @author admin
 *
 */
public class OrderDao extends HibernateDaoSupport {

	// Dao层保存订单的方法
	public Integer save(Order order) {
		Integer oid = (Integer) this.getHibernateTemplate().save(order);
		return oid;
	}
	
	// Dao层根据用户的id查询订单的数量
	public int findCountByUid(Integer uid) {
		String hql = "select count(*) from Order o where o.user.uid = ?";
		List<Long> list = this.getHibernateTemplate().find(hql, uid);
		if(list != null && list.size() > 0){
			return list.get(0).intValue();
		}
		return 0;
	}

	// Dao层根据用户的id分页查询订单
	public List<Order> findByUidWithPage(Integer uid, int begin, int limit) {
		String hql = "from Order o where o.user.uid = ? order by ordertime desc";
		List<Order> list = this.getHibernateTemplate().execute(new PageHibernateCallback<Order>(hql, new Object[]{uid}, begin, limit));
		if(list != null && list.size() > 0){
			return list;
		}
		return null;
	}

	// Dao层根据订单id查询订单
	public Order findByOid(Integer oid) {
		Order order = this.getHibernateTemplate().get(Order.class, oid);
		return order;
	}

	// Dao层更新订单的信息
	public void update(Order currOrder) {
		this.getHibernateTemplate().update(currOrder);
	}

	// Dao层查询订单的总数量
	public int findCount() {
		String hql = "select count(*) from Order";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if(list != null && list.size() > 0){
			return list.get(0).intValue();
		}
		return 0;
	}

	// Dao层分页查询所有订单
	public List<Order> findWithPage(int begin, int limit) {
		String hql = "from Order order by ordertime desc";
		List<Order> list = this.getHibernateTemplate().execute(new PageHibernateCallback<Order>(hql, null, begin, limit));
		if(list != null && list.size() > 0){
			return list;
		}
		return null;
	}

	// Dao层根据订单id查询订单项
	public List<Orderitem> findOrderItemByOid(Integer oid) {
		String hql = "from Orderitem oi where oi.order.oid = ?";
		List<Orderitem> list = this.getHibernateTemplate().find(hql, oid);
		if(list != null && list.size() > 0){
			return list;
		}
		return null;
	}

}
